package com.shujia.day12;

import java.util.TreeMap;

public class Code19TreeMap {
    public static void main(String[] args) {
        /*
            TreeMap：
                1、其中的Key是使用红黑树的形式对其进行保存，那么就具有排序的特性，其特点和TreeSet中的特点一致
                       因为 TreeSet底层是调用TreeMap中的put方法

                2、使用自定义对象作为Key
                    错误：
                    java.lang.ClassCastException: com.shujia.day12.Code19TreeMap$Student
                        cannot be cast to java.lang.Comparable
         */

        TreeMap<Integer, String> treeMap = new TreeMap<>();


        treeMap.put(1,"奖励一台电脑");
        treeMap.put(3,"奖励一个对象");
        treeMap.put(2,"奖励一部手机");

        System.out.println(treeMap);


        TreeMap<Student, String> stuTreeMap = new TreeMap<>();


        stuTreeMap.put(new Code19TreeMap.Student("杜泗洋",560),"铜陵学院");
        stuTreeMap.put(new Code19TreeMap.Student("王瑞",580),"阜阳师范");
        stuTreeMap.put(new Code19TreeMap.Student("袁亮亮",600),"宿州学员");


        System.out.println(stuTreeMap);






    }

    static class Student implements Comparable<Student>{
        String name;
        int score;

        public Student() {
        }

        public Student(String name, int score) {
            this.name = name;
            this.score = score;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", score=" + score +
                    '}';
        }


        @Override
        public int compareTo(Student that) {
            int ageRes = this.score - that.score;
            return ageRes == 0 ? this.name.compareTo(that.name): ageRes;
        }
    }
}
